import math
import threading

from HUD import HUD_deal


class HUD_control:
    msg = None
    video = None
    HUD_deal = None

    update_switch = False
    update_thread = None

    def get_output(self):
        return self.HUD_deal.get_output()

    def __init__(self, msg=None, video=None):
        self.msg = msg
        self.video = video
        self.HUD_deal = HUD_deal.HUD()
        self.HUD_deal.setBt(70)
        self.HUD_deal.setSpeed(60)

    def update_m_v(self):
        self.HUD_deal.img = self.video.img
        self.HUD_deal.setAng(self.msg.yaw)
        self.HUD_deal.setRoll(self.msg.roll)
        self.HUD_deal.setPitch(self.msg.pitch)
        self.HUD_deal.setAltitude(self.msg.tof)
        self.HUD_deal.setSpeed(int(
            math.sqrt(self.msg.vgx * self.msg.vgx + self.msg.vgy * self.msg.vgy + self.msg.vgz * self.msg.vgz)))

    def update_img(self):
        while self.update_switch:
            self.update_m_v()
            self.HUD_deal.add_attitude()

    def start(self):
        while self.video.img is None:
            pass
        self.HUD_deal.set_img(self.video.img)
        if self.update_thread is None:
            self.update_thread = threading.Thread(target=self.update_img)
        self.update_switch = True
        self.update_thread.start()

    def stop_update(self):
        self.update_switch = False
